import sys
from PyQt6.QtWidgets import  QTreeWidget, QTreeWidgetItem
from PyQt6.QtGui import QFont, QBrush, QColor
from PyQt6.QtCore import QObject

class LeftNavigationItem(QObject):
    item_dbms='DBMS'
    item_dbms_operate='DBMS数据操作'
    item_dbms_db_instance='数据库实例'
    item_redis='redis'
    item_redis_operate='redis数据操作'
    item_redis_mgr='redis管理'
    item_mq='MQ'
    item_kafka='kafka'
 
class LeftNavigationView(QTreeWidget):
    def __init__(self, parent=None,callback=None):
        super(LeftNavigationView, self).__init__(parent)
        self.callback_on_item_clicked=callback
        # 初始化树形结构
        self.initUI()

        self.setMaximumWidth(200)
 
    def initUI(self):
        self.setHeaderHidden(True)
        # 添加顶层项
        top_level_item = QTreeWidgetItem(self)
        top_level_item.setText(0, "仪表盘")
        top_level_item.setFont(0, QFont('Arial', 12))  # 设置字体和大小
        #top_level_item.setBackground(0, QBrush(QColor(200, 150, 255)))  # 设置背景色
        top_level_item.setExpanded(True)
 
        # 添加子项--DBMS
        child_item_dbms = QTreeWidgetItem()
        child_item_dbms.setText(0, LeftNavigationItem.item_dbms)
        child_item_dbms.setFont(0, QFont('Courier', 10))
        child_item_dbms.setExpanded(True)
        #child_item.setBackground(0, QBrush(QColor(150, 200, 150)))
        top_level_item.addChild(child_item_dbms)

        # 添加子项--数据操作
        child_item = QTreeWidgetItem()
        child_item.setText(0, LeftNavigationItem.item_dbms_operate)
        child_item.setFont(0, QFont('Courier', 10))
        #_item.setBackground(0, QBrush(QColor(150, 200, 150)))
        child_item_dbms.addChild(child_item)

         # 添加子项--数据库实例
        child_item = QTreeWidgetItem()
        child_item.setText(0, LeftNavigationItem.item_dbms_db_instance)
        child_item.setFont(0, QFont('Courier', 10))
        #_item.setBackground(0, QBrush(QColor(150, 200, 150)))
        child_item_dbms.addChild(child_item)

         # 添加子项--redis
        child_item_redis = QTreeWidgetItem()
        child_item_redis.setText(0, LeftNavigationItem.item_redis)
        child_item_redis.setFont(0, QFont('Courier', 10))
        #child_item.setBackground(0, QBrush(QColor(150, 200, 150)))
        top_level_item.addChild(child_item_redis)

        # 添加子项--数据操作
        child_item = QTreeWidgetItem()
        child_item.setText(0, LeftNavigationItem.item_redis_operate)
        child_item.setFont(0, QFont('Courier', 10))
        #_item.setBackground(0, QBrush(QColor(150, 200, 150)))
        child_item_redis.addChild(child_item)

         # 添加子项--redis管理
        child_item = QTreeWidgetItem()
        child_item.setText(0, LeftNavigationItem.item_redis_mgr)
        child_item.setFont(0, QFont('Courier', 10))
        #_item.setBackground(0, QBrush(QColor(150, 200, 150)))
        child_item_redis.addChild(child_item)

        # 添加子项--mq
        child_item_mq = QTreeWidgetItem()
        child_item_mq.setText(0, LeftNavigationItem.item_mq)
        child_item_mq.setFont(0, QFont('Courier', 10))
        #child_item.setBackground(0, QBrush(QColor(150, 200, 150)))
        top_level_item.addChild(child_item_mq)

         # 添加子项--kafka
        child_item_kafka = QTreeWidgetItem()
        child_item_kafka.setText(0, LeftNavigationItem.item_kafka)
        child_item_kafka.setFont(0, QFont('Courier', 10))
        #child_item.setBackground(0, QBrush(QColor(150, 200, 150)))
        top_level_item.addChild(child_item_kafka)
 
        # 添加到树中
        self.addTopLevelItem(top_level_item)

        self.itemClicked.connect(self.on_item_clicked)

    def on_item_clicked(self, item):
        # 这里处理点击事件
        print(f"Item clicked: {item.text(0)}") 
        if self.callback_on_item_clicked:
            self.callback_on_item_clicked(item.text(0))
    